package tag.in.activities;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

import tag.in.Constants;
import tag.in.R;
import tag.in.TagInApplication;
import tag.in.data.Tag;

/**
 * Show Tag details for the tag item the user selected.
 * 
 * @author andre
 */
public class TagDetailActivity extends Activity {

    private static final String CLASSTAG = TagDetailActivity.class.getSimpleName();
    private static final int MENU_MAP_REVIEW = Menu.FIRST; 
    
    private String imageLink;
    private TextView name;
    private TextView owner;
    private TextView category;
    private TextView info;
    private TextView location;
    private TextView state;
    private ImageView tagImage;
    private ImageView tagCategory;
    
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if ((imageLink != null) && !imageLink.equals("")) {
                try {
                    URL url = new URL(imageLink);
                    URLConnection conn = url.openConnection();
                    conn.connect();
                    BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
                    Bitmap bm = BitmapFactory.decodeStream(bis);
                    bis.close();
                    tagImage.setImageBitmap(bm);
                } catch (IOException e) {
                    Log.e(Constants.LOGTAG, " " + TagDetailActivity.CLASSTAG, e);
                }
            } else {
                tagImage.setImageResource(R.drawable.no_tag_image);
            }
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.v(Constants.LOGTAG, " " + TagDetailActivity.CLASSTAG + " onCreate");
        // inflate layout
        this.setContentView(R.layout.tag_detail);
        // reference XML defined views that we will touch in code
        this.name = (TextView) findViewById(R.id.name_detail);
        //this.name.startAnimation(AnimationUtils.loadAnimation(this, R.anim.scaler));
        this.owner = (TextView) findViewById(R.id.owner_detail);
        this.category = (TextView) findViewById(R.id.category_detail);
        this.info = (TextView) findViewById(R.id.info_detail);
        this.location = (TextView) findViewById(R.id.location_detail);
        this.state = (TextView) findViewById(R.id.state_detail);
        this.tagImage = (ImageView) findViewById(R.id.tag_image);
        this.tagCategory = (ImageView) findViewById(R.id.category_image);
        
        // get the current review from the Application (global state placed there)
        TagInApplication application = (TagInApplication) getApplication();
        Tag currentTag = application.getCurrentTag();
        
        this.imageLink = currentTag.imgurl;
	    this.name.setText(currentTag.name);
	    this.owner.setText(currentTag.owner);
	    this.category.setText(application.getTagCategories()[currentTag.category]);
	    this.info.setText(currentTag.info);
	    this.location.setText(currentTag.lat+","+currentTag.lon);
	    this.state.setText(currentTag.state);
	    
	    switch(currentTag.category){
	    case 0:
	    	tagCategory.setImageResource(R.drawable.warningsign);
	    	break;
	    case 1:
	    	tagCategory.setImageResource(R.drawable.problemsign);
	    	break;
	    case 2:
	    	tagCategory.setImageResource(R.drawable.eventsign);
	    	break;
	    case 3:
	    	tagCategory.setImageResource(R.drawable.infosign);
	    	break;
	    default:
	    	tagCategory.setImageResource(R.drawable.nosign);
	    	break;
	    }
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        Log.v(Constants.LOGTAG, " " + TagDetailActivity.CLASSTAG + " onResume");
        // tell handler to load image
        this.handler.sendEmptyMessage(1);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, TagDetailActivity.MENU_MAP_REVIEW, 0, R.string.menu_map_review).setIcon(
            android.R.drawable.ic_menu_mapmode);
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        Intent intent = null;
        switch (item.getItemId()) {
            case MENU_MAP_REVIEW:
                Log.v(Constants.LOGTAG, " " + TagDetailActivity.CLASSTAG + " MAP ");
                if ((this.location.getText() != null) && !this.location.getText().equals("")) {
                    /*intent = new Intent(Intent.ACTION_VIEW, Uri
                        .parse("geo:0,0?q=" + this.location.getText().toString()));
                    startActivity(intent);*/
                	intent = new Intent(Constants.INTENT_ACTION_VIEW_MAP);
                    startActivity(intent);
                } else {
                    new AlertDialog.Builder(this).setTitle(getResources().getString(R.string.alert_label)).setMessage(
                        R.string.no_location_message).setPositiveButton("Continuar", new OnClickListener() {
                        public void onClick(final DialogInterface dialog, final int arg1) {
                        }
                    }).show();
                }
                return true;
        }
        return super.onMenuItemSelected(featureId, item);
    }
}
